﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 基础
{

    //调试需要设置 调试为外部应用程序，就是cad 了
    public class LineDemo
    {
        [CommandMethod("linedemo")]
        public void Line()
        {
            // netload
            Line line = new Line();
            line.StartPoint = new Point3d(100, 100, 0); ;
            line.EndPoint = new Point3d(200, 200, 0);


            var l2=new Line(new Point3d(100,0,0),new Point3d(200,0,0));
            var l3 = new Line(new Point3d(200, 0 ,0), new Point3d(200, 200, 0));

            // 声明图形对象
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db=doc.Database;
            // 开启事务
            using (Transaction trans=db.TransactionManager.StartTransaction())
            {
                //打开块表
                var bt =(BlockTable) trans.GetObject(db.BlockTableId,OpenMode.ForRead);
                //打开块表记录
                var btr= (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
                //将直线插入到记录
                btr.AppendEntity(line);
                btr.AppendEntity(l2);
                btr.AppendEntity(l3);
                //更新
                trans.AddNewlyCreatedDBObject(line,true);
                trans.AddNewlyCreatedDBObject(l2, true);
                trans.AddNewlyCreatedDBObject(l3, true);
                //事务提交
                trans.Commit();

            }

        }

    }
}
